package com.fjh.service;

import com.fjh.repository.TeacherRepository;
import com.fjh.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class TeacherService {

    @Autowired
    private TeacherRepository teacherRepository;

    public Teacher addTeacher(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    public List<Teacher> findAllTeachers() {
        return teacherRepository.findAll();
    }

    public Optional<Teacher> findTeacherById(Long id) {
        return teacherRepository.findById(id);
    }

    public List<Teacher> getTeachersBySubject(String subject) {
        return teacherRepository.findBySubject(subject);
    }

    public List<Teacher> getTeachersBySchool(String school) {
        return teacherRepository.findBySchool(school);
    }

    public Teacher getTeacherByLoginId(Long loginId) {
        return teacherRepository.findByLoginId(loginId);
    }

    public List<Teacher> searchTeachersByName(String teacherName) {
        return teacherRepository.findByTeacherNameContaining(teacherName);
    }

    public void deleteTeacher(Long id) {
        teacherRepository.deleteById(id);
    }

    public Teacher updateTeacher(Teacher teacher) {
        return teacherRepository.save(teacher);
    }
}

